package com.example.k8s.hello.service.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class SaTokenConfig implements WebMvcConfigurer {
    public static final List<String> EXCLUDE = new ArrayList<>(Arrays.asList("/doc.html/**", "/webjars/**", "/v3/**", "/actuator/**", "/druid/**", "/favicon.ico", "/error"));

    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            SaRouter.match("/**")
                    .notMatch(SaHttpMethod.OPTIONS)
                    .check(r -> StpUtil.checkLogin());
        })).addPathPatterns("/**").excludePathPatterns(EXCLUDE);
    }
}